//
//  JKRRecommendCategoryCellTableViewCell.m
//  百思不得姐
//
//  Created by Lucky on 16/3/1.
//  Copyright © 2016年 Lucky. All rights reserved.
//

#import "JKRRecommendCategoryCellTableViewCell.h"

@interface JKRRecommendCategoryCellTableViewCell ()

@property (nonatomic, strong) UILabel *nameLabel;

@property (nonatomic, strong) UIView *bottomLine;

@property (nonatomic, strong) UIView *selectedLine;

@end

@implementation JKRRecommendCategoryCellTableViewCell

+ (CGFloat)heightWithModel:(JKRRecommendCategory *)model
{
    JKRRecommendCategoryCellTableViewCell *cell = [[JKRRecommendCategoryCellTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@""];
    cell.model = model;

    [cell layoutIfNeeded];
    
    CGRect frame = cell.nameLabel.frame;
    return frame.origin.y + frame.size.height + 1 + 10;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = JKRColor(244, 244, 244, 1.0);
        
        [self.contentView addSubview:self.nameLabel];
        [self.contentView addSubview:self.bottomLine];
        [self.contentView addSubview:self.selectedLine];
        
        [self.selectedLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView);
            make.leading.equalTo(self.contentView);
            make.width.equalTo(@5);
            make.bottom.equalTo(self.contentView).offset(-1);
        }];
        
        [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).offset(10);
            make.leading.equalTo(self.contentView);
            make.trailing.equalTo(self.contentView);
        }];
        
        [self.bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.nameLabel.mas_bottom).offset(10);
            make.height.equalTo(@1);
            make.leading.equalTo(self.contentView);
            make.trailing.equalTo(self.contentView);
        }];
    }
    return self;
}

- (void)setModel:(JKRRecommendCategory *)model
{
    _model = model;
    
    self.nameLabel.text = _model.name;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    self.selectedLine.hidden = !selected;
    self.nameLabel.textColor = selected ? self.selectedLine.backgroundColor : JKRColor(78, 78, 78, 1.0);
}

- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.text = @"测试";
        _nameLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _nameLabel;
}

- (UIView *)bottomLine {
    if (!_bottomLine) {
        _bottomLine = [[UIView alloc] init];
        _bottomLine.backgroundColor = [UIColor whiteColor];
    }
    return _bottomLine;
}

- (UIView *)selectedLine {
    if (!_selectedLine) {
        _selectedLine = [[UIView alloc] init];
        _selectedLine.backgroundColor = JKRColor(219, 21, 26, 1.0);
    }
    return _selectedLine;
}

@end
